package com.inspur.gs.sbssdl.sbwhgl.bpbjgl.bpbjgl.back.bizactions;

import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import javax.xml.crypto.dsig.spec.XPathType;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class bpbjrkglbpbjrkWriteBackMgrAction extends AbstractManagerAction<VoidActionResult> {
    private String dataId;
    private String jlz;

    public bpbjrkglbpbjrkWriteBackMgrAction(IBEManagerContext managerContext, String dataId, String jlz) {
        super(managerContext);
        this.dataId = dataId;
        this.jlz = jlz;
    }

    @Override
    public void execute() {
        /**
         * 入库回写
         * 1.如果入库的备品备件与台账有唯一相同，则数量累加
         * 2.如果入库的备件与台账不同，则新增一条台账记录
         * 3.入库统计刀台账记录中
         */
        if (jlz.equals("back")) {
            return;
        }
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            String rkid = "609b14b4-111f-4e9d-9399-fbe699bc182e";
            String tzid = "3f443799-4bc1-409f-a28d-f04742273899";
            IStandardLcp rklcp = iLcpFactory.createLcpByBEId(rkid);
            IStandardLcp tzlcp = iLcpFactory.createLcpByBEId(tzid);

            IEntityDataCollection rkmxList = rklcp.retrieve(dataId).getData().getChilds().get("rkmx");
            rkmxList.stream().forEach(rkmx -> {
                String wlbh = rkmx.getValue("wlbh").toString();
                String gcbh = rkmx.getValue("gcbh").toString();
                String wlmc = rkmx.getValue("wlmc").toString();
                String wlgg = rkmx.getValue("wlgg").toString();
                String wlxh = rkmx.getValue("wlxh").toString();
                String wlcz = rkmx.getValue("wlcz").toString();
                BigDecimal rksl = (BigDecimal) rkmx.getValue("rksl");
                String dw = rkmx.getValue("dw").toString();
                String wzlxid = ((IValuesContainer)rkmx.getValue("wzlxid")).getValue("wzlxid").toString();
                String ck = rkmx.getValue("ck").toString();
                String jsgg = rkmx.getValue("jsgg").toString();
                String cjs = rkmx.getValue("cjs").toString();
                String zlbxsx = rkmx.getValue("zlbxsx").toString();
                String iszn = rkmx.getValue("iszn").toString();
                String jysyfx = rkmx.getValue("jysyfx").toString();
                String glbm =  ((IValuesContainer)rkmx.getValue("glbm")).getValue("glbm").toString();


                // 判断台账中是否有此物料编号
                EntityFilter filter = new EntityFilter();
                ArrayList<FilterCondition> filterConditions = (ArrayList<FilterCondition>)
                        FilterCondition.builder().orEqual("wlbh", wlbh).build();
                filter.addFilterConditions(filterConditions);

                List<IEntityData> query = tzlcp.query(filter);

                if (query.isEmpty()) {
                    // 查询为空，需要新增台账数据
                    IEntityData newData = tzlcp.retrieveDefault();
                    ModifyChangeDetail changeDetail = new ModifyChangeDetail(newData.getID());
                    changeDetail.getPropertyChanges().put("wlbh",wlbh);
                    changeDetail.getPropertyChanges().put("gcbh",gcbh);
                    changeDetail.getPropertyChanges().put("wlmc",wlmc);
                    changeDetail.getPropertyChanges().put("wlgg",wlgg);
                    changeDetail.getPropertyChanges().put("wlxh",wlxh);
                    changeDetail.getPropertyChanges().put("wlcz",wlcz);
                    changeDetail.getPropertyChanges().put("lksl",rksl);
                    changeDetail.getPropertyChanges().put("dw",dw);
                    //物资类型
                    AssociationInfo wzlxidInfo  = new AssociationInfo();
                    wzlxidInfo.setValue("wzlxid",wzlxid);
                    changeDetail.getPropertyChanges().put("wzlxid", wzlxidInfo);
                    changeDetail.getPropertyChanges().put("ck",ck);
                    changeDetail.getPropertyChanges().put("jsgg",jsgg);
                    changeDetail.getPropertyChanges().put("cjs",cjs);
                    changeDetail.getPropertyChanges().put("zlbxsx",zlbxsx);
                    changeDetail.getPropertyChanges().put("iszn",iszn);
                    changeDetail.getPropertyChanges().put("jysyfx",jysyfx);
                    //管理部门
                    AssociationInfo glbmInfo  = new AssociationInfo();
                    wzlxidInfo.setValue("glbm",glbm);
                    changeDetail.getPropertyChanges().put("glbm", glbmInfo);

                    //新增一条子表数据
                    ArrayList<String> nodeCodes = new ArrayList();
                    nodeCodes.add("bgjl");
                    ArrayList<String> hierarchyIds = new ArrayList();
                    hierarchyIds.add(newData.getID());
                    IEntityData tzbgjl = tzlcp.retrieveDefaultChild(nodeCodes, hierarchyIds);
                    //子表关联
                    ModifyChangeDetail childChange = new ModifyChangeDetail(tzbgjl.getID());
                    childChange.getPropertyChanges().put("bglx", "入库");
                    childChange.getPropertyChanges().put("sl", rksl);
                    AssociationInfo bgbmInfo  = new AssociationInfo();
                    bgbmInfo.setValue("bgbm",glbm);
                    childChange.getPropertyChanges().put("bgbm", bgbmInfo);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
                    try {
                        Date parse = formatter.parse(formatter.format(new Date()));
                        childChange.getPropertyChanges().put("bgsj",parse);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    //变更值
                    changeDetail.addChildChangeSet("bgjl", childChange);
                    tzlcp.modify(changeDetail);
                } else {
                    String id = query.get(0).getID();
                    BigDecimal lksl = (BigDecimal) tzlcp.retrieve(id).getData().getValue("lksl");
                    ModifyChangeDetail changeDetail = new ModifyChangeDetail(id);
                    changeDetail.getPropertyChanges().put("lksl", lksl.add(rksl));
                    //子表关联
                    ModifyChangeDetail childChange = new ModifyChangeDetail(id);
                    childChange.getPropertyChanges().put("bglx", "入库");
                    childChange.getPropertyChanges().put("sl", rksl);
                    AssociationInfo bgbmInfo  = new AssociationInfo();
                    bgbmInfo.setValue("bgbm",glbm);
                    childChange.getPropertyChanges().put("bgbm", bgbmInfo);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
                    try {
                        Date parse = formatter.parse(formatter.format(new Date()));
                        childChange.getPropertyChanges().put("bgsj",parse);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    //变更值
                    changeDetail.addChildChangeSet("bgjl", childChange);
                    tzlcp.modify(changeDetail);
                }
                tzlcp.save();
            });

        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }
    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}